﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Telerik.Web.UI;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.GERP.GHRMS.APP.Classes;

namespace KPIS.GERP.GHRMS.APP.App_UI.Forms
{
    public partial class MP_PositionCommands : System.Web.UI.Page
    {
        private SsMng ssMng = new SsMng();
        private IPositionCommandBLL CmdPos = new PositionCommandBLL();
        private IEmployeeTypeBLL emptypeBLL = new EmployeeTypeBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                FillDataToEmpType();
                LoadDataGrid();
            }
        }
        public void LoadDataGrid()
        {
            Session["EMPTYPE"] = cmbEmpType.SelectedValue;
            Nullable<int> EmpTypeSeq = cmbEmpType.SelectedValue.Length > 0 ? Int32.Parse(cmbEmpType.SelectedValue) : (int?)null;

            IList<PositionCommandInfo> Info = CmdPos.LoadGridPositionCommands(EmpTypeSeq);
            RadGrid1.DataSource = Info;

            GridGroupByExpression g = new GridGroupByExpression(RadGrid1.Columns[8]);
            RadGrid1.MasterTableView.GroupByExpressions.Add(g);

            RadGrid1.DataBind();
        }
        public void FillDataToEmpType()
        {
            IList<EmployeeTypeInfo> InfoList = emptypeBLL.ListEmployeeTypes(null);

            cmbEmpType.DataSource = InfoList;
            cmbEmpType.DataBind();
        }
        protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
        {
            PositionCommandInfo Info = new PositionCommandInfo();

            if (e.CommandName == "DeleteAll")
            {
                GridItem[] items = RadGrid1.MasterTableView.GetItems(GridItemType.CommandItem);
                if (items.Length > 0)
                {
                    ((GridCommandItem)items[0]).FireCommandEvent("DeleteSelected", "");
                }
                else
                {
                    RadGrid1.Items[0].FireCommandEvent("DeleteSelected", "");
                }
            }
            else if (e.CommandName == "Delete")
            {
                string Seq = (e.Item as GridDataItem).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["Seq"].ToString();

                Info.Seq = Seq.Length > 0 ? Int32.Parse(Seq) : (int?)null;
                Info.UpdatedBy = ssMng.EmpSeqI;

                CmdPos.Delete(Info);
            }
                        
            LoadDataGrid();            
        }

        protected void cmbEmpType_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            LoadDataGrid();
        }

   }
}
